package com.dj.product.k2faas.service.datasource;

import com.dj.product.k2faas.dto.datasource.AbstractDataSourceRequest;
import com.dj.product.k2faas.dto.datasource.CreateDataSourceRequest;
import com.dj.product.k2faas.dto.datasource.DataSourceResponse;
import com.dj.product.k2faas.dto.datasource.UpdateDataSourceRequest;
import com.dj.product.k2faas.entity.datasource.DataSource;
import com.dj.product.k2faas.common.enums.DataStatus;
import com.dj.product.k2faas.common.utils.BeanUtil;
import com.dj.product.k2faas.jpa.utils.DataSourceUtil;
import com.dj.product.k2faas.view.datasource.DataSourceView;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by 丁建 on 2025/9/23 09:27
 *
 * @author 丁建
 */
@Transactional
@Service
class DefaultDataSourceService implements DataSourceService {

    @Value("${logging.level.net.ttddyy.dsproxy.listener.logging:DEBUG}")
    private String sqlDebugLevel;

    @Autowired
    private DataSourceView dataSourceView;

    @Override
    public void testDataSource(AbstractDataSourceRequest request) {
        boolean proxy = sqlDebugLevel.equalsIgnoreCase("debug");
        HikariDataSource hikariDataSource = DataSourceUtil.getDataSource(request.getType(), request.getUrl(), request.getUsername(), request.getPassword(),
                                                                         request.getConnectionTimeout(), request.getMaxPoolSize(), request.getMinimumIdle(),
                                                                         request.getIdleTimeout(), request.getMaxLifetime(), request.isAutoCommit(), proxy);
        new JdbcTemplate(hikariDataSource).queryForMap(request.getType().getConnectionInitSql());
        hikariDataSource.close();
    }

    @Override
    public long add(CreateDataSourceRequest dataSource) {
        var ds = BeanUtil.convert(dataSource, DataSource.class).setStatus(DataStatus.NORMAL).setDefaultConnection(false);
        ds = dataSourceView.save(ds);
        return ds.getId();
    }

    @Override
    public void edit(UpdateDataSourceRequest dataSource) {
        var ds = dataSourceView.findById(dataSource.getId());
        BeanUtil.copyProperties(dataSource, ds);

        dataSourceView.save(ds);
    }

    @Override
    public DataSourceResponse findById(long id) {
        var ds = dataSourceView.findById(id);
        return BeanUtil.convert(ds, DataSourceResponse.class);
    }

    @Override
    public Page<DataSourceResponse> findAll(String keyword, Pageable pageable) {
        var pageInfo = dataSourceView.findAll(keyword, pageable);

        return BeanUtil.toPage(pageInfo, DataSourceResponse.class, pageable);
    }

    @Override
    public void changeStatus(long id, DataStatus dataStatus) {
        var ds = dataSourceView.findById(id).setStatus(dataStatus);
        dataSourceView.save(ds);
    }

    @Override
    public void setAsDefault(long id) {
        dataSourceView.updateAllAsNotDefault();

        var ds = dataSourceView.findById(id).setDefaultConnection(true);
        dataSourceView.save(ds);
    }
}
